

package com.hoshiicloud.common.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import com.hoshiicloud.common.exception.HoshiiCloudException;

/**
 * <p> 摘要加密工具类 </p>
 *
 * @author yl
 */
public abstract class DigestUtils {

    /**
     * MD5加密
     *
     * @param data 待加密数据
     * @return 加密后的数据
     */
    public static String md5(String data) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(data.getBytes());
            return HexUtils.encodeHexStr(md.digest());
        } catch (NoSuchAlgorithmException e) {
            throw new HoshiiCloudException("Failed to get the Message MD5 digest instance", e);
        }

    }

    /**
     * MD5加密
     *
     * @param data 待加密数据
     * @return 加密后的数据
     */
    public static String md5(byte[] data) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(data);
            return HexUtils.encodeHexStr(md.digest());
        } catch (NoSuchAlgorithmException e) {
            throw new HoshiiCloudException("Failed to get the Message MD5 digest instance", e);
        }
    }

    /**
     * SHA加密
     *
     * @param data 待加密数据
     * @return 加密后的数据
     */
    public static final String sha(String data) {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(data.getBytes());
            return HexUtils.encodeHexStr(md.digest());
        } catch (NoSuchAlgorithmException e) {
            throw new HoshiiCloudException("Failed to get the Message SHA digest instance", e);
        }
    }

    /**
     * SHA加密
     *
     * @param data 待加密数据
     * @return 加密后的数据
     */
    public static final String sha(byte[] data) {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(data);
            return HexUtils.encodeHexStr(md.digest());
        } catch (NoSuchAlgorithmException e) {
            throw new HoshiiCloudException("Failed to get the Message SHA digest instance", e);
        }
    }
}
